<?php
  class RegisterController extends Controller
  {
  	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

  	  public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	public function accessRules()
	{
		return array(

//			array('deny', // allow admin user to perform 'admin' and 'delete' actions
//				'actions'=>array('index'),
//				'expression'	=>	array($this, 'lognined'),
//			),

//			array('deny',  // deny all users
//				'users'=>array('*'),
//			),
		);
	}

	public function lognined(CWebUser $user, CAccessRule $rule){
//		echo '<pre>', print_r(!empty(Yii::app()->user->id), true), '</pre>';die;
        if (!empty(Yii::app()->user->id))
            $this->redirect('/site/index');
        else{
			return false;
        }

	}

  	  public function actionIndex()
  	  {
  	  	  // Refesh captcha
		$captchaAction = $this->createAction('captcha');
		$captchaAction->getVerifyCode(true);
        $model = new RegisterForm();
		if(isset($_POST['register'])){
			$username=$_POST['RegisterForm']['username'];
			$password=$_POST['RegisterForm']['password'];
			$phone=$_POST['RegisterForm']['phone'];
			$email=$_POST['RegisterForm']['email'];
			$name=$_POST['RegisterForm']['name'];
			$question=$_POST['RegisterForm']['question'];
			$answer=$_POST['RegisterForm']['answer'];

            $model->setAttributes($_POST['RegisterForm'], false);

			if($this->checkUser($username)){
			    $account = new Account();

                $account->loginName=$username;
                $account->password_hash=md5($password);
                $account->cPhone=$phone;
                $account->cEMail=$email;
                $account->cRealName=$name;
                $account->cQuestion=$question;
                $account->cAnswer=$answer;
                $account->Coin=0;
                $account->lockpassword='123456';

                $account->save();

                Yii::app()->user->setFlash('success',Yii::t('label','Đăng ký thành công! <br> Bạn có thể đăng nhập tại đây.'));
                $this->redirect('/site/login');
			}

		}


	  	$this->render('index',array(
	  		'model'=>$model,
	  	));
  	  }

      public function checkUser($username)
      {
            if(strlen($username) < 6){
                Yii::app()->user->setFlash('error','label','Tài khoản quá ngắn');
                return false;
            }
            if(strlen($username) > 24){
                Yii::app()->user->setFlash('error','label','Tài khoản quá dài');
               return false;
            }
               if(!preg_match('/^[a-z]*$/',$username)){
               Yii::app()->user->setFlash('error','tài khoản chỉ được dùng các ký tự a-z');
                return false;
            }
            $account = Account::model()->findByAttributes(array('loginName'=>$username));
            if(!empty($account)){
                Yii::app()->user->setFlash('error','tài khoản đã tồn tại');
                return false;
            }
            return true;
      }
  }
